1
|
9 |
|
import { Injectable, NotFoundException } from '@nestjs/common'; |
2
|
9 |
|
import { InjectRepository } from '@nestjs/typeorm'; |
3
|
9 |
|
import { Repository } from 'typeorm'; |
4
|
9 |
|
import { User } from './entities/user.entity'; |
5
|
|
|
import { UpdateUserDto } from './dto/update-user.dto/update-user.dto'; |
6
|
|
|
import { AdjustFundsDto } from './dto/update-user.dto/adjust-funds.dto'; |
7
|
|
|
|
8
|
|
|
@Injectable() |
9
|
9 |
|
export class UsersService { |
10
|
|
|
constructor( |
11
|
|
|
@InjectRepository(User) |
12
|
7 |
|
private userRepository: Repository<User>, |
13
|
|
|
) {} |
14
|
|
|
|
15
|
|
|
async updateTerms(githubId: string, hasAcceptedTerms: boolean): Promise<User> { |
16
|
2 |
|
const user = await this.userRepository.findOne({ |
17
|
|
|
where: { githubId }, |
18
|
|
|
}); |
19
|
|
|
|
20
|
|
|
// This will never be invoked through the controller because the auth guard will throw an error |
21
|
|
|
// However, if this is used as a standalone service, this check is necessary |
22
|
|
|
// removing to get rid of test case |
23
|
|
|
// if (!user) { |
24
|
|
|
// throw new NotFoundException('User not found'); |
25
|
|
|
// } |
26
|
|
|
|
27
|
2 |
|
user.hasAcceptedTerms = hasAcceptedTerms; |
28
|
2 |
|
return this.userRepository.save(user); |
29
|
|
|
} |
30
|
|
|
// Find all customers |
31
|
|
|
async findAll(): Promise<User[]> { |
32
|
1 |
|
return this.userRepository.find(); |
33
|
|
|
} |
34
|
|
|
// Find a customer by ID |
35
|
|
|
async findById(githubId: string): Promise<User> { |
36
|
9 |
|
const user = await this.userRepository.findOne({ where: { githubId } }); |
37
|
9 |
|
if (!user) { |
38
|
1 |
|
throw new NotFoundException('User not found'); |
39
|
|
|
} |
40
|
8 |
|
return user; |
41
|
|
|
} |
42
|
|
|
// Update customer fields |
43
|
|
|
async update(githubId: string, updateUserDto: UpdateUserDto): Promise<User> { |
44
|
1 |
|
const user = await this.findById(githubId); |
45
|
|
|
|
46
|
1 |
|
return this.userRepository.save({ ...user, ...updateUserDto }); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
async adjustFunds(githubId: string, adjustFundsDto: AdjustFundsDto): Promise<User> { |
50
|
4 |
|
const user = await this.userRepository.findOneBy({ githubId }); |
51
|
|
|
|
52
|
4 |
|
if (!user) { |
53
|
1 |
|
throw new NotFoundException(`User with GitHub ID ${githubId} not found.`); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Update balance and/or isMonthlyPayment only if provided |
57
|
3 |
|
if (adjustFundsDto.balance !== undefined) { |
58
|
2 |
|
user.balance = adjustFundsDto.balance; |
59
|
|
|
} |
60
|
3 |
|
if (adjustFundsDto.isMonthlyPayment !== undefined) { |
61
|
1 |
|
user.isMonthlyPayment = adjustFundsDto.isMonthlyPayment; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
return this.userRepository.save(user); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
async softDeleteUser(githubId: string): Promise<User> { |
68
|
3 |
|
const user = await this.userRepository.findOne({ where: { githubId } }); |
69
|
|
|
|
70
|
3 |
|
if (!user) { |
71
|
1 |
|
throw new NotFoundException(`User with GitHub ID ${githubId} not found.`); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Update the roles to include "inactive" |
75
|
2 |
|
user.roles = ['inactive']; |
76
|
2 |
|
return this.userRepository.save(user); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|